package com.ie.sevice.before;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ie.common.http.ResponseResult;
import com.ie.common.security.utils.ConfigrarionBean;
import com.ie.common.security.utils.JwtTokenUtil;
import com.ie.common.security.utils.MD5Util;
import com.ie.common.security.utils.RedisUtil;
import com.ie.entity.BUserEntity;
import com.ie.mapper.before.BUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BUserServiceImpl extends ServiceImpl<BUserMapper, BUserEntity> implements BUserService {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private ConfigrarionBean config;

    @Override
    public ResponseResult<Map<String, String>> register(BUserEntity bUserEntity) {
        bUserEntity.setBpwd(MD5Util.MD5(bUserEntity.getBpwd()));
        Long count = lambdaQuery().eq(BUserEntity::getBemail, bUserEntity.getBemail()).count();
        if (count > 0) {
            return ResponseResult.getMessageResult(null, "A001");
        } else if (save(bUserEntity)) {
            return ResponseResult.getMessageResult(null, "A002");
        } else
            return ResponseResult.getMessageResult(null, "A003");
    }

    @Override
    public ResponseResult<Map<String, String>> login(BUserEntity bUserEntity) {
        String code = (String) redisUtil.get("code");
        // 验证码错误
        if (!code.equalsIgnoreCase(bUserEntity.getCode())) {
            return ResponseResult.getMessageResult(null, "A000");
        }
        // lambdaQuery().eq(BUserEntity::getBemail, bUserEntity.getBemail()).eq(BUserEntity::getBpwd, MD5Util.MD5(bUserEntity.getBpwd())).one();
        Long count = lambdaQuery().eq(BUserEntity::getBemail, bUserEntity.getBemail()).count();
        if (count == 0) {// 用户名不存在
            return ResponseResult.getMessageResult(null, "A001");
        }
        bUserEntity.setBpwd(MD5Util.MD5(bUserEntity.getBpwd()));
        List<BUserEntity> list = lambdaQuery()
                .eq(BUserEntity::getBemail, bUserEntity.getBemail())
                .eq(BUserEntity::getBpwd, bUserEntity.getBpwd()).list();
        if (list.size() > 0) {
            String token = jwtTokenUtil.createToken(bUserEntity.getBemail());
            // redisUtil.set("login_" + bUserEntity.getBemail(), bUserEntity.getBemail(), config.getExpiration());
            Map<String, String> myres = new HashMap<>();
            myres.put("buserauthtoken", token);
            myres.put("bemail", bUserEntity.getBemail());
            // myres.put("bid", list.get(0) + "");
            // return ResponseResult.getSuccessResult(myres);

            redisUtil.set("login_" + bUserEntity.getBemail(), bUserEntity.getBemail(), config.getRedisExpiration());
            // Map<String, String> myres = new HashMap<>();
            // 返回包含token、用户名和用户ID的成功响应
            // myres.put("buserauthtoken", token);
            // myres.put("bemail", bUserEntity.getBemail());
            // 存放的是用户ID
            myres.put("bid", list.get(0).getId() + "");
            return ResponseResult.getSuccessResult(myres);
        } else {
            return ResponseResult.getMessageResult(null, "A002");
        }
    }
}
